package com.airline.action;

import java.io.PrintWriter;
import java.rmi.RemoteException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.airline.beans.Customer;
import com.airline.beans.Employee;
import com.airline.beans.Flight;
import com.airline.beans.Journey;
import com.airline.connection.AirlineSystemServiceProxy;

public class JsonAction extends DispatchAction {
	AirlineSystemServiceProxy proxy = new AirlineSystemServiceProxy();
	
	
	
	public ActionForward searchemployee(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest req, HttpServletResponse response) throws Exception {

		
		Employee searchEmp = (Employee) req.getSession().getAttribute("searchemp");
		
		
		String rows = req.getParameter("rows");
		Integer irows = Integer.parseInt(rows)-1000;
		
		Employee[] listemp = proxy.searchEmployees(searchEmp,String.valueOf(irows),"1000");
	
		
		
		JSONObject masterObject = new JSONObject();	
		JSONArray masterArray = new JSONArray();
	
		if(listemp!= null){
		
		for(Employee emp : listemp){
			JSONObject rowObject = new JSONObject();
			JSONArray cellArray = new JSONArray();
			cellArray.add(emp.getPersonID());
			cellArray.add(emp.getLastName());
			cellArray.add(emp.getFirstName());
			cellArray.add(emp.getPosition());
			cellArray.add(emp.getWorkDescription());
			cellArray.add(emp.getHireDate());
			cellArray.add(emp.getAddress());
			cellArray.add(emp.getCity());
			cellArray.add(emp.getState());
			
			rowObject.put("cell",cellArray);
			rowObject.put("id",emp.getPersonID());
			
			masterArray.add(rowObject);
		}
		
		
		masterObject.put("rows", masterArray);
		}
		
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		out.println(masterObject);
		out.flush();
		
		return null;
	}
	
	public ActionForward listjourneys(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		
	JSONObject masterObject = new JSONObject();	
	JSONArray masterArray = new JSONArray();
	
	String rows = request.getParameter("rows");
	Integer irows = Integer.parseInt(rows)-1000;
	Journey[] listemp = null;
	
	try {
		listemp = proxy.getAllJourneys();
	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	if(listemp != null){
	
	for(Journey journey : listemp){
		JSONObject rowObject = new JSONObject();
		JSONArray cellArray = new JSONArray();
		
		cellArray.add(journey.getCustomerID());
		cellArray.add(journey.getFirstFlightNumber());
		
		if(journey.getSecondFlightNumber().equals("0")){
		cellArray.add("Direct Flight");
		}else
			cellArray.add(journey.getSecondFlightNumber());
		cellArray.add(journey.getBoardingPoint());
		
		cellArray.add(journey.getIntermediatePoint());
		cellArray.add(journey.getDestination());
		cellArray.add(journey.getPaymentOption());
		cellArray.add(journey.getTotalPrice());
	
		
		rowObject.put("cell",cellArray);
		rowObject.put("id",journey.getJourneyID());
		
		masterArray.add(rowObject);
	}
	
	}
	
	masterObject.put("rows", masterArray);
	
	masterObject.put("page", 1);
	
	masterObject.put("total", masterArray.size());
	masterObject.put("records", masterArray.size());
	
	
	
	
	response.setContentType("application/json");
	PrintWriter out = response.getWriter();
	  out.println(masterObject);
	out.flush();
	//response.getWriter().write(userDataObject.toJSONString());
	
	
	

		return null;
	}
	public ActionForward listemployees(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		
	JSONObject masterObject = new JSONObject();	
	JSONArray masterArray = new JSONArray();
	
	String rows = request.getParameter("rows");
	Integer irows = Integer.parseInt(rows)-1000;
	
	Employee[] listemp = null;
	
	try {
		listemp = proxy.getAllEmployees(String.valueOf(irows),"1000");
	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
	for(Employee emp : listemp){
		
		JSONObject rowObject = new JSONObject();
		JSONArray cellArray = new JSONArray();
		cellArray.add(emp.getPersonID());
		cellArray.add(emp.getLastName());
		cellArray.add(emp.getFirstName());
		cellArray.add(emp.getPosition());
		cellArray.add(emp.getWorkDescription());
		cellArray.add(emp.getHireDate());
		cellArray.add(emp.getAddress());
		cellArray.add(emp.getCity());
		cellArray.add(emp.getState());
		
		rowObject.put("cell",cellArray);
		rowObject.put("id",emp.getPersonID());
		
		masterArray.add(rowObject);
		
	}
	
	
	
	masterObject.put("rows", masterArray);
	
	masterObject.put("page", "1");
	
	masterObject.put("total", "1");
	masterObject.put("records", masterArray.size());
	
	
	/*JSONObject userDataObject = new JSONObject();	
	
	userDataObject.put("mydata1", masterObject);*/
	//response.getOutputStream().write(userDataObject)
	response.setContentType("application/json");
	PrintWriter out = response.getWriter();
	  out.println(masterObject);
	out.flush();
	//response.getWriter().write(userDataObject.toJSONString());
	
	
	
	
		return null;
	}
	public ActionForward listcustomers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		
	JSONObject masterObject = new JSONObject();	
	JSONArray masterArray = new JSONArray();
	
	String rows = request.getParameter("rows");
	Integer irows = Integer.parseInt(rows)-1000;
	
	Customer[] listemp = null;
	
	try {
		listemp = proxy.getAllCustomers(String.valueOf(irows),"1000");
	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
	for(Customer emp : listemp){
		JSONObject rowObject = new JSONObject();
		JSONArray cellArray = new JSONArray();
		cellArray.add(emp.getPersonID());
		cellArray.add(emp.getLastName());
		cellArray.add(emp.getFirstName());
		cellArray.add(emp.getPassportNumber());
		cellArray.add(emp.getNationality());
		cellArray.add(emp.getDateOfBirth());
		cellArray.add(emp.getAddress());
		cellArray.add(emp.getCity());
		cellArray.add(emp.getState());
		
		rowObject.put("cell",cellArray);
		rowObject.put("id",emp.getPersonID());
		
		masterArray.add(rowObject);
	}
	
	
	
	
	
	
	
	
	
	masterObject.put("rows", masterArray);
	
	masterObject.put("page", 1);
	
	masterObject.put("total", masterArray.size());
	masterObject.put("records", masterArray.size());
	
	
	/*JSONObject userDataObject = new JSONObject();	
	
	userDataObject.put("mydata1", masterObject);*/
	//response.getOutputStream().write(userDataObject)
	response.setContentType("application/json");
	PrintWriter out = response.getWriter();
	  out.println(masterObject);
	out.flush();
	//response.getWriter().write(userDataObject.toJSONString());
	
	
	
	
		return null;
	}

	public ActionForward listflights(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		
	JSONObject masterObject = new JSONObject();	
	JSONArray masterArray = new JSONArray();
	
	String rows = request.getParameter("rows");
	Integer irows = Integer.parseInt(rows)-1000;
	Flight[] listemp = null;
	
	try {
		listemp = proxy.getAllFlights(String.valueOf(irows),"1000");
	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
	for(Flight flight : listemp){
		JSONObject rowObject = new JSONObject();
		JSONArray cellArray = new JSONArray();
		cellArray.add(flight.getFlightNumber());
		cellArray.add(flight.getSource());
		cellArray.add(flight.getDestination());
		cellArray.add(flight.getBoardTime());
		cellArray.add(flight.getDestTime());
		cellArray.add(flight.getAirlineName());
		cellArray.add(flight.getAvailableSeatCount());
		cellArray.add(flight.getCrewDetails());
		cellArray.add(flight.getPrice());
		
		rowObject.put("cell",cellArray);
		rowObject.put("id",flight.getFlightID());
		
		masterArray.add(rowObject);
	}
	
	
	
	masterObject.put("rows", masterArray);
	
	masterObject.put("page", 1);
	
	masterObject.put("total", masterArray.size());
	masterObject.put("records", masterArray.size());
	
	
	
	response.setContentType("application/json");
	PrintWriter out = response.getWriter();
	  out.println(masterObject);
	out.flush();
	//response.getWriter().write(userDataObject.toJSONString());
	
	
	
	
		return null;
	}

	public ActionForward listcustomersbyflightid(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	

		
	String flightid = (String) request.getSession().getAttribute("flightId");
	
	
	JSONObject masterObject = new JSONObject();	
	JSONArray masterArray = new JSONArray();
	
	String rows = request.getParameter("rows");
	Integer irows = Integer.parseInt(rows)-1000;
	
	Customer[] listemp = null;
	
	try {
		listemp = proxy.getAllCustomersByFlightID(flightid);
	} catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	if(listemp != null){
	
	for(Customer emp : listemp){
		JSONObject rowObject = new JSONObject();
		JSONArray cellArray = new JSONArray();
		cellArray.add(emp.getPersonID());
		cellArray.add(emp.getLastName());
		cellArray.add(emp.getFirstName());
		cellArray.add(emp.getPassportNumber());
		cellArray.add(emp.getNationality());
		cellArray.add(emp.getDateOfBirth());
		cellArray.add(emp.getAddress());
		cellArray.add(emp.getCity());
		cellArray.add(emp.getState());
		
		rowObject.put("cell",cellArray);
		rowObject.put("id",emp.getPersonID());
		
		masterArray.add(rowObject);
	}
	
	}
	
	masterObject.put("rows", masterArray);
	
	masterObject.put("page", 1);
	
	masterObject.put("total", masterArray.size());
	masterObject.put("records", masterArray.size());
	
	
	/*JSONObject userDataObject = new JSONObject();	
	
	userDataObject.put("mydata1", masterObject);*/
	//response.getOutputStream().write(userDataObject)
	response.setContentType("application/json");
	PrintWriter out = response.getWriter();
	  out.println(masterObject);
	out.flush();
	//response.getWriter().write(userDataObject.toJSONString());
	
	
	
	
		return null;
	}

	public ActionForward searchflights(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest req, HttpServletResponse response) throws Exception {
	
		
		Flight flightTemp = (Flight) req.getSession().getAttribute("searchflight");
		
		
		JSONObject masterObject = new JSONObject();	
		JSONArray masterArray = new JSONArray();
		
		String rows = req.getParameter("rows");
		Integer irows = Integer.parseInt(rows)-1000;
		Flight[] listemp = null;
		
		try {
			listemp = proxy.searchFlights(flightTemp, String.valueOf(irows), "1000");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	if(listemp != null){	
		
		for(Flight flight : listemp){
			JSONObject rowObject = new JSONObject();
			JSONArray cellArray = new JSONArray();
			cellArray.add(flight.getFlightNumber());
			cellArray.add(flight.getSource());
			cellArray.add(flight.getDestination());
			cellArray.add(flight.getBoardTime());
			cellArray.add(flight.getDestTime());
			cellArray.add(flight.getAirlineName());
			cellArray.add(flight.getAvailableSeatCount());
			cellArray.add(flight.getCrewDetails());
			cellArray.add(flight.getPrice());
			
			rowObject.put("cell",cellArray);
			rowObject.put("id",flight.getFlightID());
			
			masterArray.add(rowObject);
		}
		
		
	}
		masterObject.put("rows", masterArray);
		
		masterObject.put("page", 1);
		
		masterObject.put("total", masterArray.size());
		masterObject.put("records", masterArray.size());
		
		
		
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		  out.println(masterObject);
		out.flush();
		//response.getWriter().write(userDataObject.toJSONString());
		
		
		
		
			return null;
		
	}

}
